﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Agents.Admin
{
    public partial class ShowAgent : System.Web.UI.Page
    {
        //this page shows a given agents working information
        //the information is for administrater avaluation
        protected void Page_Load(object sender, EventArgs e)
        {
            int agentId = Convert.ToInt32(Request.QueryString["agentId"]);
            Queries queries = new Queries();
            Agents agent = queries.GetAgent(agentId);

            //spilling agents data onto screen
            nameTxtbox.Text = agent.Name;
            phoneTxtbox.Text = agent.Phone;
            usernameTxtbox.Text = agent.UserName;

            idTxtbox.Text = agent.TZ;

            areaTxtbox.Text = agent.Regions.Name;
            emailTxtbox.Text = agent.EMail;
            ordersInShkalimMonth.Text = agent.Mincome.ToString();
            ordersInShkalimWeek.Text = agent.Wincome.ToString();

            // Show only two numbers after the decimal point
            averageMeetingTimeMonth.Text = agent.MavgTime.GetValueOrDefault().ToString("F");
            averageMeetingTimeWeek.Text = agent.WavgTime.GetValueOrDefault().ToString("F");
            numberOfOrdersMonth.Text = agent.Morders.ToString();
            numberOfOrdersWeek.Text = agent.Worders.ToString();
            numberOfWorkingHoursMonth.Text = agent.Mhours.GetValueOrDefault().ToString("F");
            numberOfWorkingHoursWeek.Text = agent.Whours.GetValueOrDefault().ToString("F");
        }

        //function returns a list of the last month's dates
        public Dictionary<DateTime, string> GetLastMonth()
        {
            //creating date list 
            Dictionary<DateTime, string> list = new Dictionary<DateTime, string>();
            //creating date
            DateTime dt = new DateTime();
            //giving the date of today
            dt = DateTime.Now;
            //going over the last 30 days and adding the dates to the list
            for (int i = 0; i > -30; i--)
            {
                dt = dt.AddDays(i);
                list.Add(dt, dt.ToString("D"));
                dt = DateTime.Now;

            }
            //list.Sort();
            return list;
        }

        //redirecting the administrator back to his homepage
        protected void backBtn_Click(object sender, EventArgs e)
        {
            string name = Request.QueryString["name"];
            Response.Redirect("~/Admin/ChooseEditAgent.aspx");
        }

        //function findes the log file for the agent on a given date
        //function redirects to the page where the information will be displayed
        protected void btnWatchLog_Click(object sender, EventArgs e)
        {
            //retrieving the date chosen from combo
            string date = txtSelectedDate.Text;
            
            //retrieving agents name
            int agentId = Convert.ToInt32(Request.QueryString["agentId"]);
            Queries queries = new Queries();
            Agents agent = queries.GetAgent(agentId);
            string name = agent.Name;

            Response.Redirect("AgentFile.aspx?agentId=" + agentId + "&date=" + date);
        }
    }
}